<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-7-15
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */

class NewAction extends Action{
    private $M_news;

    private $A_category;
    private $A_source;

    private $upload;
    /**
     * 构造方法
     * 初始化
     */
    public function __construct(){
        $this->M_news = Model::getInstance('news');

        $this->A_category = Action::getInstance('category');
        $this->A_source = Action::getInstance('source');

        $this->upload = new Upload();

    }

    public function index(){

        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);
        $title = $this->input()->post('title');
        $status = $this->input()->post('status');
        $filter = array();

        if($title){
            $filter['title'] = $title;
        }

        if($status){
            $filter['status'] = $status;
        }

        $category_list = $this->A_category->getCategorys(null,true);
        $source_list = $this->A_source->getSources(null,true);

        $list = $this->M_news->getNewList($curr_page,$per_page,$filter);

        $data = array(
            'list'=>$list,
            'category_list'=>$category_list,
            'source_list'=>$source_list
        );
        Template::show('New/index.php',$data);

    }

    public function add(){
        $category_list = $this->A_category->getCategorys(null,true);
        $source_list = $this->A_source->getSources(null,true);

        $data = array(
            'category_list'=>$category_list,
            'source_list'=>$source_list
        );

        Template::show('New/add.php',$data);
    }

    public function del(){
        $new_id = $this->input()->get('new_id');

        $ret = $this->M_news->delNew($new_id);

        if($ret){
            $data = array(
                "statusCode"=>"200",
                "message"=>"删除成功",
                "navTabId"=>"new",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"删除失败",
            );
            echo Response::JSON(300,$data);
        }
    }

    public function edit(){
        $new_id = $this->input()->get('new_id');

        $info = $this->M_news->get($new_id);

        $category_list = $this->A_category->getCategorys(null,true);
        $source_list = $this->A_source->getSources(null,true);

        $data = array(
            'info'=>$info,
            'category_list'=>$category_list,
            'source_list'=>$source_list
        );

        Template::show('New/edit.php',$data);
    }

    public function recommendIndex(){

    }

    public function recommendList(){

    }

    public function ajaxAdd(){

        $category_id = $this->input()->post('category_id');
        $source_id = $this->input()->post('source_id');
        $title = $this->input()->post('title');
        $keywords = $this->input()->post('keywords');
        $description = $this->input()->post('description');
        $status = $this->input()->post('status');
        $summary = $this->input()->post('summary');
        $content = $this->input()->post('content');
        $tag = $this->input()->post('tag');
        $picture = $this->input()->post('picture');
        $add_user_id = $this->session()->get('user_id');
        $edit_user_id = $this->session()->get('user_id');
        $recommend = $this->input()->post('recommend',1);
        $recommend_index = $this->input()->post('recommend_index',1);

        if(!$category_id || !$source_id || !$title || !$keywords || !$description || !$status || !$summary || !$content || !$tag || !$picture || !$add_user_id || !$edit_user_id){
            $data = array(
                "statusCode"=>"300",
                "message"=>"参数错误，请填写完入库表单！",
            );
            echo Response::JSON(300,$data);
            exit;
        }

        $ret = $this->M_news->addNew($category_id, $source_id, $title, $keywords, $description, $status, $summary, $content, $tag, $picture, $add_user_id, $edit_user_id, $recommend, $recommend_index);
        if($ret){

            $data = array(
                "statusCode"=>"200",
                "message"=>"添加成功",
                "navTabId"=>"new",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"添加失败",
            );
            echo Response::JSON(300,$data);
        }
        exit;
    }

    /**
     * 新闻 缩图 上传
     */
    public function newPictureUpload(){
        $this->upload->maxSize  = 10485760 ;
        //设置需要生成缩略图，仅对图像文件有效
        $this->upload->thumb = true;
        //设置需要生成缩略图的文件后缀
        $this->upload->thumbPrefix = '_thumb';  //生产2张缩略图
        //设置缩略图最大宽度
        $this->upload->thumbMaxWidth = '320';
        //设置缩略图最大高度
        $this->upload->thumbMaxHeight = '200';

        $curr_picture = $this->input()->post('curr_picture');
        //$curr_picture = null;
        if($curr_picture){
            $file_pos = strrpos($curr_picture,'/');

            $picture_info = explode('.',substr($curr_picture,$file_pos));

            $thumb_basename = $picture_info[0];
            $this->upload->thumbFile = $thumb_basename;
            $thumb_extension = $picture_info[1];
            $this->upload->thumbExt = $thumb_extension;

            $dir = substr($curr_picture,0,$file_pos);
            //上传图片路径
            $this->upload->savePath =  str_replace('thumb','temp',$dir);
            //设置缩略图路径
            $this->upload->thumbPath =  $dir;
        }else{
            $this->upload->thumbFile = date('YmdHis') . '_' . rand(1,999999999);
            $dir =  '../uploads/news/temp/'.date('Y').'/'.date('m').'/'.date('d').'/';
            //上传图片路径
            $this->upload->savePath = $dir;
            //设置缩略图路径
            $this->upload->thumbPath =  str_replace('temp', 'thumb', $dir);
        }
        //删除原图
        $this->upload->thumbRemoveOrigin = true;

        if(!$this->upload->uploadFile()) {
            $data = array(
                "statusCode"=>"300",
                "message"=>$this->upload->getErrorMsg(),
            );
            echo Response::JSON(300,$data);
        }else{
            $info =  $this->upload->getUploadFileInfo();

            $thumb_ext = pathinfo($info[0]['savename']);

            echo $this->upload->thumbPath.$this->upload->thumbFile.'.'.$thumb_ext['extension'];
            setcookie('on_upload_success',$this->upload->thumbPath.$this->upload->thumbFile.'.'.$thumb_ext['extension'], time()+3600*24, '/', CURR_DOMAIN);

        }
    }
}